package edu.base.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.base.model.CatDetail;
import edu.base.model.Category;
import edu.base.model.CmtJoinUser;
import edu.base.model.MaintenanceRecord;
import edu.base.model.MaintenanceRecordJoinUsers;
import edu.base.service.CatDetailService;
import edu.base.service.CategoryService;
import edu.base.service.CmtService;
import edu.base.service.MaintenanceRecordService;
import edu.base.service.UsersService;
import edu.base.util.PageUtil;

@Controller
@RequestMapping("/worker")
public class WorkerController {
	final static org.slf4j.Logger log = LoggerFactory.getLogger(WorkerController.class);

	@Autowired
	UsersService usersService; 
	@Autowired
	CategoryService categoryService;
	@Autowired
	CatDetailService catDetailService;
	@Autowired
	MaintenanceRecordService maintenanceRecordService;
	@Autowired
	CmtService cmtService;
	
	
	@RequestMapping("index")
	public String index() {
		return "/worker/index";
	}

	@RequestMapping("welcome")
	public String welcome() {
		return "/admin/welcome";
	}
	
	@RequestMapping("record/lst")
	public String recordLst(Integer recordId,Integer p,String startDate, String endDate,Integer state,Integer workerId,Integer customerId,Model model,HttpSession session) throws ParseException {
		Date date1=null;
		Date date2 = null;
		if(startDate != null) {
			date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
		}
		if(endDate != null) {
			date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
		}
		if(p == null) {
			p = 1;
		}
		List<MaintenanceRecordJoinUsers> list = new ArrayList<MaintenanceRecordJoinUsers>();
		if(recordId != null) {
			return "redirect:/worker/record/detail?id=" + recordId;
		}
		
		list = maintenanceRecordService.query(date1, date2, customerId, workerId, state, p, 5);
		Integer count = maintenanceRecordService.count(date1, date2, customerId, workerId, state);
		String url = "/worker/record/lst?";
		if(recordId !=null ) {
			url += "&recordId=" + recordId;
		}
		if(startDate != null) {
			url += "&startDate=" + startDate;
		}
		if(endDate != null) {
			url += "&endDate=" + endDate;
		}
		if(state != null) {
			url += "&state=" + state;
		}
		if(customerId != null) {
			url += "&customerId=" + customerId;
		}
		if(workerId != null) {
			url += "&workerId=" + workerId;
		}
		PageUtil page = new PageUtil(5, count, p, url);
		model.addAttribute("page", page.page());
		
		model.addAttribute("list", list);
		model.addAttribute("p", p);
		model.addAttribute("startDate",startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("state", state);
		model.addAttribute("customerId", customerId);
		model.addAttribute("workerId", workerId);
		model.addAttribute("recordId", recordId);
		
		model.addAttribute("userId",(Integer)session.getAttribute("user_id"));
		log.error("userId:" + session.getAttribute("user_id"));
		return "/worker/record/lst";
	}
	
	@RequestMapping("record/jiedan")
	public String recordJiedan(Integer id,HttpSession session,Model model) {
		
		Integer userId = (Integer) session.getAttribute("user_id");
		MaintenanceRecord item = new MaintenanceRecord();
		item.setRecordId(id);
		item.setWorkerId(userId);
		item.setState(2);
		maintenanceRecordService.update(item);
		
		return "redirect:/worker/record/lst?state=2&workerId=" + userId ;
	}
	
	@RequestMapping("record/detail")
	public String RecordDetail(Integer id,HttpSession session,Model model) {
		Integer userId = (Integer)session.getAttribute("user_id");
		MaintenanceRecord item = maintenanceRecordService.findByKey(id);
		List<CatDetail> lst = catDetailService.find(null, null, false, id);
		List<Category> catLst = categoryService.query(0);
		model.addAttribute("lst", lst);
		model.addAttribute("userId",userId);
		model.addAttribute("item", item);
		model.addAttribute("catLst", catLst);
		List<CmtJoinUser> cmtLst = cmtService.topicCmt(id);
		
		model.addAttribute("cmt_lst", cmtLst);		
		return "/worker/record/detail";
	}
	
	@RequestMapping("record/finished")
	public String recordFinished(Integer id,BigDecimal labor, HttpSession session,Model model) {
		MaintenanceRecord item = new MaintenanceRecord();
		item.setRecordId(id);
		item.setEndDate(new Date());
		item.setState(4);
		//获取人工费用
		item.setLabor(labor);
		//获取零件费用
		List<CatDetail> lst = catDetailService.find(null, null, false, id);
		BigDecimal material = new BigDecimal(0);
		for(int x =0 ; x < lst.size(); x++) {
			material = material.add(lst.get(x).getInPrice());
		}
		item.setMaterial(material);
		item.setCount(material.add(labor));
		maintenanceRecordService.update(item);
		log.error("完工:" + item.toString());
		return "redirect:/worker/record/detail?id=" + id;
	}
	
	@RequestMapping("record/material_add")
	public String recordMaterialAdd(Integer id, Integer mid) {
		CatDetail item = new CatDetail();
		item.setDetailId(mid);
		item.setToId(id);
		item.setState(2);
		catDetailService.update(item);
		//todo 更新库存category表
		CatDetail detail = catDetailService.findByKey(mid);
		Integer catId = detail.getCatId();
		Integer count = catDetailService.count(catId, 1, false, null);
		Category cat = new Category();
		cat.setCatId(catId);
		cat.setCurCount(count);
		categoryService.update(cat);
		return "redirect:/worker/record/detail?id=" + id;
	}
}
